package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.ViolationRecordBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.FileUploadDao;
import com.sp.scmdb.dao.ViolationRecordDao;
import com.sp.scmdb.entity.FileUpload;
import com.sp.scmdb.entity.ViolationRecord;
import com.sp.scmdb.service.ViolationRecordService;

@Service("violationRecordService")
public class ViolationRecordServiceImpl implements ViolationRecordService {

	private ViolationRecordDao violationRecordDao;
	private FileUploadDao fileUploadDao;
	
	public void setViolationRecordDao(ViolationRecordDao violationRecordDao) {
		this.violationRecordDao = violationRecordDao;
	}

	public void setFileUploadDao(FileUploadDao fileUploadDao) {
		this.fileUploadDao = fileUploadDao;
	}

	@Override
	@Transactional
	public String createViolationRecord(ViolationRecordBean violationRecord) {
		String result = "2";
		ViolationRecord vr = new ViolationRecord();
		vr.setTitle(violationRecord.getTitle());
		vr.setContent(violationRecord.getContent());
		vr.setEmployee(violationRecord.getEmployee());
		vr.setViolationDate(violationRecord.getViolationDate());
		vr.setGrade(violationRecord.getGrade());
		vr.setIllegalType(violationRecord.getIllegalType());
		vr.setCreatedDate(violationRecord.getCreatedDate());
		vr.setUser(violationRecord.getUser());
		vr.setRectification(violationRecord.getRectification());
		vr.setDescription(violationRecord.getDescription());
		vr.setVtype(violationRecord.getVtype());
		vr.setState(violationRecord.getState());
		vr.setAttachmentId(violationRecord.getAttachmentId());
		
		violationRecordDao.save(vr);
		result = CommonParamter.CHECK_SUCCESS;
		return result;
	}

	@Override
	@Transactional
	public String delViolationRecord(String[] arr) {
		String result = CommonParamter.CHECK_FAIL;
		ViolationRecord vr = null;
		for (int i = 0; i < arr.length; i++) {
			vr = violationRecordDao.getBySysId(arr[i]);
			violationRecordDao.delete(vr);
		}
		result = CommonParamter.CHECK_SUCCESS;
		return result;
	}

	@Override
	@Transactional
	public String editViolationRecord(ViolationRecordBean violationRecord) {
		String result = CommonParamter.CHECK_FAIL;
		ViolationRecord vr = null;
		vr = violationRecordDao.getBySysId(violationRecord.getSysId());
		vr.setTitle(violationRecord.getTitle());
		vr.setContent(violationRecord.getContent());
		vr.setEmployee(violationRecord.getEmployee());
		vr.setViolationDate(violationRecord.getViolationDate());
		vr.setGrade(violationRecord.getGrade());
		vr.setIllegalType(violationRecord.getIllegalType());
		vr.setRectification(violationRecord.getRectification());
		vr.setDescription(violationRecord.getDescription());
		vr.setVtype(violationRecord.getVtype());
		vr.setState(violationRecord.getState());
		
		violationRecordDao.update(vr);
		result = CommonParamter.CHECK_SUCCESS;
		return result;
	}

	@Override
	public List<ViolationRecordBean> getListViolationRecord(String hql) {
		ViolationRecordBean vrBean = null;
		ViolationRecord vr = null;
		String _hql = "from  ViolationRecord where 1 = 1 order by createdDate desc";
		List<ViolationRecord> list = violationRecordDao.queryHql(_hql);
		List<ViolationRecordBean> vrList = new ArrayList<ViolationRecordBean>();
		
		try {
			for (int i = 0; i < list.size(); i++) {
				vr = list.get(i);
				vrBean = new ViolationRecordBean();
				vrBean.setSysId(vr.getSysId());
				vrBean.setTitle(vr.getTitle());
				vrBean.setContent(vr.getContent());
				vrBean.setEmployee(vr.getEmployee());
				vrBean.setViolationDate(vr.getViolationDate());
				vrBean.setGrade(vr.getGrade());
				String illegalTypeIds = vr.getIllegalType();
				if(StringUtils.isNotBlank(illegalTypeIds)) {
					String[] str = illegalTypeIds.split(",");
					StringBuffer strBuffer = new StringBuffer();
					for(int j = 0; j < str.length; j++) {
						getIllegalTypeNames(strBuffer, str[j]);
					}
					
					vrBean.setIllegalTypeName(strBuffer.toString());
				}
				vrBean.setIllegalType(illegalTypeIds);
				vrBean.setCreatedDate(vr.getCreatedDate());
				vrBean.setUser(vr.getUser());
				vrBean.setRectification(vr.getRectification());
				vrBean.setDescription(vr.getDescription());
				vrBean.setVtype(vr.getVtype());
				vrBean.setState(vr.getState());
				vrBean.setAttachmentId(vr.getAttachmentId());
				
				List<FileUpload> fileList = fileUploadDao.queryHql("from FileUpload where entityId = '" + vr.getAttachmentId() + "' ");
				vrBean.setFileList(fileList);
				
				vrList.add(vrBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vrList;
	}

	@Override
	public ViolationRecordBean getViolationRecord(String sysId) {
		ViolationRecord vr = violationRecordDao.getBySysId(sysId);
		ViolationRecordBean vrBean = null;
		try {
			if(vr != null) {
				vrBean = new ViolationRecordBean();
				vrBean.setSysId(vr.getSysId());
				vrBean.setTitle(vr.getTitle());
				vrBean.setContent(vr.getContent());
				vrBean.setEmployee(vr.getEmployee());
				vrBean.setViolationDate(vr.getViolationDate());
				vrBean.setGrade(vr.getGrade());
				vrBean.setIllegalType(vr.getIllegalType());
				vrBean.setCreatedDate(vr.getCreatedDate());
				vrBean.setUser(vr.getUser());
				vrBean.setRectification(vr.getRectification());
				vrBean.setDescription(vr.getDescription());
				vrBean.setVtype(vr.getVtype());
				vrBean.setState(vr.getState());
				vrBean.setAttachmentId(vr.getAttachmentId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vrBean;
	}
	
	private void getIllegalTypeNames(StringBuffer strBuffer, String illegalTypeId) {
		if(illegalTypeId.equals("1")) strBuffer.append("作业人员劳动防护用品未按要求佩戴     ");
		if(illegalTypeId.equals("2")) strBuffer.append("作业人员未经过安全教育培训上岗     ");
		if(illegalTypeId.equals("3")) strBuffer.append("作业人员未正确使用工具、设施    ");
		if(illegalTypeId.equals("4")) strBuffer.append("作业人员不遵守现场规章制度    ");
		if(illegalTypeId.equals("5")) strBuffer.append("施工机械设备未按规定检测或验收就已使用的    ");
		if(illegalTypeId.equals("6")) strBuffer.append("起重吊装违反‘十不吊’规定的    ");
		if(illegalTypeId.equals("7")) strBuffer.append("临时施工用电违反用电规范的    ");
		if(illegalTypeId.equals("8")) strBuffer.append("特殊工种作业人员无证操作的    ");
		if(illegalTypeId.equals("9")) strBuffer.append("宿舍内电线乱拖乱拉、违规使用电器的    ");
		if(illegalTypeId.equals("10")) strBuffer.append("施工现场动火不符合消防安全管理规定的    ");
		if(illegalTypeId.equals("11")) strBuffer.append("未按安全技术专项方案进行施工的    ");
		if(illegalTypeId.equals("12")) strBuffer.append("无方案擅自施工的    ");
		if(illegalTypeId.equals("13")) strBuffer.append("安全防护设施存在较大安全隐患或无安全防护设施进行作业的    ");
		if(illegalTypeId.equals("14")) strBuffer.append("脚手架、模板支撑、大型设备、临时用电等设施未经验收合格后使用的");
	}
}
